exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
    constructor() {
        super();
        this.taskModel = this.db['bpmn_hi_taskinst'];
        this.actModel = this.db['bpmn_hi_actinst'];
        this.attachmentModel = this.db['sys_attachment'];
        this.userModel = this.db['sys_user'];
        this.taskModel.hasMany(this.attachmentModel, {
            foreignKey: 'businesskey',
            targetKey: 'id'
        });
        this.taskModel.belongsTo(this.userModel, {
            foreignKey: 'assignee',
            targetKey: 'username'
        });
    }
    /**
     * 通过porcid查询案件流程信息
     * 
     * @param {any} data  data.id
     * @returns 
     */
    getTaskByProcId(data) {
        var _this = this;

        return _asyncToGenerator(function* () {
            let where = {
                proc_inst_id: data.proc_inst_id,
                task_status: 1
            };
            try {
                const Res = yield _this.taskModel.findAll({
                    where: where,
                    order: [['start_time', 'ASC']],
                    include: [
                    //     {
                    //     model: this.attachmentModel,
                    //     // as: 'Attachment',
                    //     attributes: ['file_name', 'realpath']
                    // }, 
                    {
                        model: _this.userModel,
                        attributes: ['id', 'realname']
                    }]
                });
                return Res;
            } catch (error) {
                return _this.exceptionService.handleError(error);
            }
        })();
    }
    /**
     * 通过porcid查询案件待
     *
     * @param {any} data  data.id
     * @returns
     */
    getWaitAssigneeByProcId(data) {
        var _this2 = this;

        return _asyncToGenerator(function* () {
            let where = {
                proc_inst_id: data.proc_inst_id,
                act_status: 0

            };
            try {

                let Res = yield _this2.actModel.findAll({
                    where: where,
                    order: [['start_time', 'ASC']]
                });
                for (let item of Res) {
                    if (item.assignee != '') {
                        let arr = JSON.parse(item.assignee);
                        let userIdArr = [];
                        arr.map(function (item) {
                            userIdArr.push(item);
                        });
                        item.dataValues.assignee = yield _this2.userModel.findAll({ where: { id: userIdArr } });
                    }
                }
                return Res;
            } catch (error) {
                return _this2.exceptionService.handleError(error);
            }
        })();
    }
};